//
//  VC_StudyData.m
//  yjw-winner
//
//  Created by Kae on 2018/9/5.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_StudyData.h"
#import "Cell_Course.h"


@interface Model_StudyData : NSObject
@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subs;
@property (nonatomic, copy) NSString *detail;
@property (nonatomic, assign) CGFloat cellHeight;
@end

@implementation Model_StudyData

@end

@interface VC_StudyData () <UITableViewDelegate, UITableViewDataSource>{
    UITableView *_tableView;
}
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, copy) CallBackStudyDataBlock block;
@property (nonatomic, strong) id model;
@end

@implementation VC_StudyData
- (NSArray *)dataArray {
    if (!_dataArray) {
        Model_StudyData *model0 = [[Model_StudyData alloc] init];
        Model_StudyData *model1 = [[Model_StudyData alloc] init];
        Model_StudyData *model2 = [[Model_StudyData alloc] init];
        Model_StudyData *model3 = [[Model_StudyData alloc] init];
        
        model0.identifier = @"identifier0";
        model1.identifier = @"identifier1";
        model2.identifier = @"identifier2";
        model3.identifier = @"identifier3";
        
        model0.title = @"赢家大学最强导师团:顶级商业课程合集";
        model1.title = @"课程评价 4.5分";
        model2.title = @"课程笔记";
        model3.title = @"(老师姓名)老师的金句";
        
        model0.subs = @"";
        model1.subs = @"11万次播放";
        model2.subs = @"";
        model3.subs = @"更多金句";
        
        model0.cellHeight = 90;
        model1.cellHeight = 200;
        model2.cellHeight = 150;
        model3.cellHeight = 170;
        
        _dataArray = @[model0,
                       model1,
                       model2,
                       model3];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.separatorInset = UIEdgeInsetsMake(10, 0, 20, 0);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.tableFooterView = [UIView new];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    _tableView.frame = self.view.bounds;
}


- (void)refreshWithModel:(id)model CallBackBlock:(CallBackStudyDataBlock)block {
    self.model = model;
    self.block = block;
}
#pragma mark --mark UITableViewDelegate UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model_StudyData *model = self.dataArray[indexPath.row];
    Cell_Course *cell = [tableView dequeueReusableCellWithIdentifier:model.identifier];
    if (!cell) {
        cell = [[Cell_Course alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:model.identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    __weak typeof(self) weakself = self;
    [cell refreshWithModel:model CallBackBlock:^(CellCourseButtonClickType type, UIButton *sender) {
        [weakself courseCellCallBackWith:type Button:sender];
    }];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model_StudyData *model = self.dataArray[indexPath.row];
    return model.cellHeight;
}

#pragma mark --mark 业务逻辑

- (void)courseCellCallBackWith:(CellCourseButtonClickType)type Button:(UIButton *)sender {
    switch (type) {
        case CellCourseButtonClickTypeShowMore: {
            // 展开查看更多
            [self spreadOut:sender];
            break;
        }
        case CellCourseButtonClickTypeComment: {
            // 填写评论
            [self comment:sender];
            break;
        }
        case CellCourseButtonClickTypeLookupAllComment: {
            // 查看全部评论
            [self lookupAllComment:sender];
            break;
        }
        case CellCourseButtonClickTypeMoreSentence: {
            // 查看更多金句
            [self moreSentence:sender];
            break;
        }
        default:
            break;
    }
}

/**
 展开
 */
- (void)spreadOut:(UIButton *)sender {
    Model_StudyData *model = self.dataArray[0];
    if (model.cellHeight == 90) {
        model.cellHeight = 190;
        [sender setTitle:@"展开更多" forState:UIControlStateNormal];
    }else {
        model.cellHeight = 90;
        [sender setTitle:@"收起" forState:UIControlStateNormal];
    }
    [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

/**
 评论

 @param sender sender
 */
- (void)comment:(UIButton *)sender {
    if (self.block) {
        self.block(StudyDataCellButtonClickTypeComment);
    }
}

/**
 查看全部评论

 @param sender sender
 */
- (void)lookupAllComment:(UIButton *)sender {
    if (self.block) {
        self.block(StudyDataCellButtonClickTypeLookupAllCommnet);
    }
}

/**
 更多金句

 @param sender sender
 */
- (void)moreSentence:(UIButton *)sender {
    if (self.block) {
        self.block(StudyDataCellButtonClickTypeMoreSentence);
    }
}
@end
